package com.jhx.video.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import com.jhx.video.R;

import me.dm7.barcodescanner.core.IViewFinder;
import me.dm7.barcodescanner.core.ViewFinderView;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

/**
 * @author jack
 */
public class QRCodeScanActivity extends Activity {

//    private ZXingScannerView mScannerView;
//
//    @Override
//    public void onCreate(Bundle state) {
//        super.onCreate(state);
//        ViewGroup content = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_qrcode, null);
//        setContentView(content);
//
//        mScannerView = new ZXingScannerView(this) {
//            @Override
//            protected IViewFinder createViewFinderView(Context context) {
//                return new CustomViewFinderView(context);
//            }
//        };
//        content.addView(mScannerView);
//    }
//
//    @Override
//    public void onResume() {
//        super.onResume();
//        mScannerView.setResultHandler(this);
//        mScannerView.startCamera();
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        mScannerView.stopCamera();
//    }
//
//    @Override
//    public void handleResult(Result rawResult) {
//
//        String url = rawResult.toString();
//        Intent intent = new Intent();
//        //把返回数据存入Intent
//        if (TextUtils.isEmpty(url)) {
//            intent.putExtra("result", "");
//        } else {
//            intent.putExtra("result", url);
//        }
//
//        //设置返回数据
//        setResult(RESULT_OK, intent);
//        //关闭Activity
//        finish();
//
//        // Note:
//        // * Wait 2 seconds to resume the preview.
//        // * On older devices continuously stopping and resuming camera preview can result in freezing the app.
//        // * I don't know why this is the case but I don't have the create_time to figure out.
//        Handler handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                mScannerView.resumeCameraPreview(QRCodeScanActivity.this);
//            }
//        }, 2000);
//    }
//
//    private static class CustomViewFinderView extends ViewFinderView {
//        public static final String TRADE_MARK_TEXT = "scan url";
//        public static final int TRADE_MARK_TEXT_SIZE_SP = 40;
//        public final Paint PAINT = new Paint();
//
//        public CustomViewFinderView(Context context) {
//            super(context);
//            init();
//        }
//
//        public CustomViewFinderView(Context context, AttributeSet attrs) {
//            super(context, attrs);
//            init();
//        }
//
//        private void init() {
//            PAINT.setColor(Color.WHITE);
//            PAINT.setAntiAlias(true);
//            float textPixelSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
//                    TRADE_MARK_TEXT_SIZE_SP, getResources().getDisplayMetrics());
//            PAINT.setTextSize(textPixelSize);
//        }
//
//        @Override
//        public void onDraw(Canvas canvas) {
//            super.onDraw(canvas);
//            drawTradeMark(canvas);
//        }
//
//        private void drawTradeMark(Canvas canvas) {
//            Rect framingRect = getFramingRect();
//            float tradeMarkTop;
//            float tradeMarkLeft;
//            if (framingRect != null) {
//                tradeMarkTop = framingRect.bottom + PAINT.getTextSize() + 10;
//                tradeMarkLeft = framingRect.left;
//            } else {
//                tradeMarkTop = 10;
//                tradeMarkLeft = canvas.getHeight() - PAINT.getTextSize() - 10;
//            }
//            canvas.drawText(TRADE_MARK_TEXT, tradeMarkLeft, tradeMarkTop, PAINT);
//        }
//    }
}
